"""
类名：LoginApi
实例属性：
login_url：保存登陆的url，"http://ihrm-test.itheima.net/api/sys/login"
实例方法：
    def login(self, body)：
        # 功能：发送登陆请求
        # :param body: 发送请求的请求体
        # :return: 发送登陆请求后的响应对象
"""
import unittest

import requests


class LoginApi(object):
    def __init__(self):
        self.login_url = 'http://ihrm-test.itheima.net/api/sys/login'

    def login(self, body):
        return requests.post(self.login_url, json=body)


class TestLogin(unittest.TestCase):
    def test_login_ok(self):
        body = {
            "mobile": "13800000002",
            "password": "123456"
        }

        resp = LoginApi().login(body)
        json_data = resp.json()

        # 断言
        self.assertEqual(200, resp.status_code)
        self.assertEqual(True, json_data.get('success'))
        self.assertEqual(10000, json_data.get('code'))
        self.assertIn('成功', json_data.get('message'))

    def test_user_error(self):
        body = {
            "mobile": "138000000021",
            "password": "123456"
        }

        resp = LoginApi().login(body)
        json_data = resp.json()

        # 断言
        self.assertEqual(200, resp.status_code)
        self.assertEqual(False, json_data.get('success'))
        self.assertEqual(20001, json_data.get('code'))
        self.assertIn('错误', json_data.get('message'))

    def test_pwd_error(self):
        body = {
            "mobile": "13800000002",
            "password": "1234561"
        }

        resp = LoginApi().login(body)
        json_data = resp.json()

        # 断言
        self.assertEqual(200, resp.status_code)
        self.assertEqual(False, json_data.get('success'))
        self.assertEqual(20001, json_data.get('code'))
        self.assertIn('错误', json_data.get('message'))